package com.zw.yzk.kabu.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.utils.LogUtils;
import com.zw.yzk.kabu.management.ActivityManager;
import com.zw.yzk.kabu.view.activity.CosplayDetailsActivity;
import com.zw.yzk.kabu.view.activity.ExhibitionDetailsActivity;
import com.zw.yzk.kabu.view.activity.MainActivity;
import com.zw.yzk.kabu.view.activity.SplashActivity;
import com.zw.yzk.kabu.view.activity.TicketOrderDetailsActivity;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by zhanwei on 2017/5/8.
 */

public class JPushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            return;
        }
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            LogUtils.d("zhanwei", "JPush用户注册成功");
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            LogUtils.d("zhanwei", "接受到推送下来的自定义消息");
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            LogUtils.d("zhanwei", "接受到推送下来的通知");
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            LogUtils.d("zhanwei", "用户点击打开了通知");
            openNotification(context, bundle);
        } else {
            LogUtils.d("zhanwei", "Unhandled intent - " + intent.getAction());
        }
    }

    private void openNotification(Context context, Bundle bundle) {
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtils.d("zhanwei", "extras: " + extras);
        MessageModel model;
        try {
            model = new Gson().fromJson(extras, MessageModel.class);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        if (model.kabu == null) {
            return;
        }
        Intent intent = null;
        switch (model.kabu.type) {
            case "0":
                //type=0, 表明是门票将要开始的推送，此时“id”的值为门票的订单ID；打开门票二维码页面
                intent = new Intent(context, TicketOrderDetailsActivity.class);
                intent.putExtra(ActivityConstant.EXHIBITION_TICKET_ORDER_ID, model.kabu.id);
                break;
            case "1":
                //type=1, 表明是关注的展会可以购票的推送，此时"id"的值为展会的ID, 打开展会详情
                intent = new Intent(context, ExhibitionDetailsActivity.class);
                intent.putExtra(ActivityConstant.EXHIBITION_DETAILS_ID, model.kabu.id);
                break;
            case "2":
                //type=2, 表明是门票校验成功的推送，此时“id”的值为门票的订单ID, 启动App
                if (ActivityManager.getInstance().getActivity(MainActivity.class) == null) {
                    intent = new Intent(context, SplashActivity.class);
                }
                break;
            case "3":
                //type=3, 表明是发布cos的时候@了某位用户，此时”id”的值为cosplay帖子的的ID；打开Cosplay详情页面
            case "4":
                //type=4, 表明是关注的人，发布了新的cosplay时，此时”id”的值为cosplay帖子的的ID；打开Cosplay详情页面
            case "5":
                //type=5, 表明是别人回复你的评论或者cos帖子时的推送，此时”id”的值为cosplay帖子的的ID；打开Cosplay详情页面
                intent = new Intent(context, CosplayDetailsActivity.class);
                try {
                    intent.putExtra(ActivityConstant.OPEN_COS_DETAIL_ID, Integer.parseInt(model.kabu.id));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }

    }

}
